odoo.define('nd_present.copy_clip_char_list', function (require) {

"use strict";
var pyUtils = require('web.py_utils');
var core = require('web.core');
var _t = core._t;
var registry = require('web.field_registry');
var FieldChar = require('web.basic_fields').FieldChar;

var qweb = core.qweb;


var CopyClipboard = {

    /**
     * @override
     */
    destroy: function () {
        this._super.apply(this, arguments);
        if (this.clipboard) {
            this.clipboard.destroy();
        }
    },

    //--------------------------------------------------------------------------
    // Private
    //--------------------------------------------------------------------------

    /**
     * Instatiates the Clipboad lib.
     */
    _initClipboard: function () {
        var self = this;
        var $clipboardBtn = this.$('.o_clipboard_button');
        $clipboardBtn.tooltip({title: _t('Copied !'), trigger: 'manual', placement: 'right'});
        $clipboardBtn.on('click', function(ev){ev.stopPropagation()});
        this.clipboard = new ClipboardJS($clipboardBtn[0], {
            text: function () {
                return self.value.trim();
            },
            // Container added because of Bootstrap modal that give the focus to another element.
            // We need to give to correct focus to ClipboardJS (see in ClipboardJS doc)
            // https://github.com/zenorocha/clipboard.js/issues/155
            container: self.$el[0]
        });
        this.clipboard.on('success', function () {
            _.defer(function () {
                $clipboardBtn.tooltip('show');
                _.delay(function () {
                    $clipboardBtn.tooltip('hide');
                }, 800);
            });
        });
    },
    /**
     * @override
     */
    _render: function () {
        this._super.apply(this, arguments);
        this.$el.addClass('o_field_copy_list');
    },
    /**
     * @override
     */
    _renderReadonly: function () {
        this._super.apply(this, arguments);
        if (this.value) {
            this.$el.append($(qweb.render(this.clipboardTemplate)));
            this._initClipboard();
        }
    }
};


var CopyClipboardList = FieldChar.extend(CopyClipboard, {
    clipboardTemplate: 'CopyClipboardList',
});

registry.add('CopyClipboardList', CopyClipboardList)

})